package com.ml.loan.sys.controller;

import com.ml.loan.common.annotaion.OperLog;
import com.ml.loan.common.constants.SysConstant;
import com.ml.loan.common.model.JsonResult;
import com.ml.loan.common.model.PageResult;
import com.ml.loan.sys.model.SysConfig;
import com.ml.loan.sys.service.SysConfigService;
import com.ml.loan.sys.vo.AppSwitchConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.websocket.server.PathParam;

/**
 * @param
 * @author Mickey
 * @date 2022/8/4 16:36
 * @return
 */
@Controller
@RequestMapping("/system/config")
public class SysConfigController {

    @Autowired
    private SysConfigService configService;


    @RequestMapping("/view")
    public String view(Model model) {
        return "system/config/config.html";
    }

    @RequestMapping("/appConfig")
    public String applyConfig(@PathParam("id") Integer id, Model model) {
        return "system/config/config.html";
    }

    @RequestMapping("/systemParams")
    public String systemConfig() {
        return "system/config/system_param.html";
    }

    @RequestMapping("/param/add")
    public String paramAdd(Model model, String appId) {
        model.addAttribute("appId", appId);
        return "system/config/param_add.html";
    }

    @RequestMapping("/param/edit")
    public String paramEdit(Model model, String id) {
        SysConfig config = configService.getById(id);
        model.addAttribute("conf", config);
        return "system/config/param_edit.html";
    }

    @RequestMapping("/add")
    public String add(Model model, String appId) {
        model.addAttribute("appId", appId);
        return "system/config/config_add.html";
    }

    @RequestMapping("/edit")
    public String edit(Model model, String id) {
        SysConfig config = configService.getById(id);
        model.addAttribute("conf", config);
        return "system/config/config_edit.html";
    }

    @RequestMapping("/appSwitch")
    public String appSwitch(Model model, String appId) {
        model.addAttribute("appId", appId);
        AppSwitchConfig configObj = configService.getConfigObj(SysConstant.CONFIG_KEY.APP_SWITCH, AppSwitchConfig.class);
        model.addAttribute("config", configObj == null ? new AppSwitchConfig() : configObj);
        return "system/config/app_switch.html";
    }

    @RequestMapping("/list")
    @ResponseBody
    @OperLog(module = "SYS", desc = "系统配置列表-查询")
    public PageResult<SysConfig> list(Integer page, Integer limit, String configName) {
        return configService.list(page, limit, configName);
    }


    @RequestMapping("/save")
    @ResponseBody
    @OperLog(module = "SYS", desc = "系统配置列表-新增", result = true)
    public JsonResult save(SysConfig config) {
        if (config == null || config.getConfValue() == null) {
            return JsonResult.error("参数不为空");
        }
        if (configService.checkConfigExists(config.getConfKey(), config.getId())) {
            return JsonResult.error("配置已存在");
        }

        if (configService.save(config)) {
            return JsonResult.ok();
        }
        return JsonResult.error();
    }

    @RequestMapping("/update")
    @ResponseBody
    @OperLog(module = "SYS", desc = "系统配置列表-更新", result = true)
    public JsonResult updConfig(SysConfig config) {
        if (config == null || config.getId() == null || config.getConfValue() == null) {
            return JsonResult.error("参数不为空");
        }

        if (configService.checkConfigExists(config.getConfKey(), config.getId())) {
            return JsonResult.error("配置已存在");
        }

        SysConfig before = configService.getById(config.getId());

        if (configService.updateById(config)) {
            return JsonResult.ok();
        }
        return JsonResult.error();
    }


    @RequestMapping("/delete")
    @ResponseBody
    @OperLog(module = "SYS", desc = "系统配置列表-删除", result = true)
    public JsonResult delete(Integer id) {
        if (configService.removeById(id)) {
            return JsonResult.ok();
        }
        return JsonResult.error();
    }

    @RequestMapping("/syncConfig")
    @ResponseBody
    public JsonResult syncConfig(Integer id) {
        if (configService.syncConfig(id)) {
            return JsonResult.ok("同步成功");
        }
        return JsonResult.error();
    }


    @RequestMapping("/saveSwitchConfig")
    @ResponseBody
    @OperLog(module = "SYS", desc = "系统配置列表-开关配置", result = true)
    public JsonResult saveSwitchConfig(AppSwitchConfig switchConfig) {
        return configService.saveSwitchConfig(switchConfig);
    }
}
